﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCG.SolarVengeanceEngine;
using SCG.WinForms;
using SCG.General;

namespace SCG.SolarVengeance
{
    public partial class AlliancePanel : BasePanel
    {
        //constructors
        public AlliancePanel()
            : base()
        {
            InitializeComponent();
        }

        //impulse processing
        public override void ImpulseUpdate()
        {
            //remove players that are no longer in the game
            for (int n = lvPlayers.Items.Count - 1; n >= 0; n--)
            {
                Player p = lvPlayers.Items[n].Tag as Player;
                if (!Game.Players.Contains(p) || p.Eliminated)
                    lvPlayers.Items.RemoveAt(n);
            }

            //add players that are not in the list
            foreach (Player p in Game.Players)
            {
                if (p == Game.CurrentPlayer)
                    continue;
                if (p.Eliminated)
                    continue;
                bool found = false;
                foreach(ListViewItem lvi in lvPlayers.Items)
                    if (lvi.Tag == p)
                    {
                        found = true;
                        break;
                    }
                if (!found)
                {
                    ListViewItem lvi = lvPlayers.Items.Add(p.Name);
                    lvi.Tag = p;
                    if (p.Glyph != null)
                    {
                        Image glyph = ImageUtils.ResizeBitmap(p.Glyph as Image, 60, 45, true);
                        glyphs.Images.Add(glyph);
                        lvi.ImageIndex = glyphs.Images.Count - 1;
                    }
                }
                Refresh();
            }

            //view current alliance for each player
            foreach (ListViewItem lvi in lvPlayers.Items)
            {
                Player p = lvi.Tag as Player;
                AllianceLevel myStance = Game.CurrentPlayer.AlliedWith(p);
                AllianceLevel theirStance = p.AlliedWith(Game.CurrentPlayer);
                lvi.StateImageIndex = ((int)myStance) * 4 + (int)theirStance - 1;
            }
        }

        //selected a player
        private bool _settingAllianceValue = false;
        private void lvPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvPlayers.SelectedItems.Count != 1)
            {
                btnNone.Visible = false;
                btnFriendly.Visible = false;
                btnIntel.Visible = false;
                btnFormal.Visible = false;
                Refresh();
                return;
            }
            else
            {
                Player p = lvPlayers.SelectedItems[0].Tag as Player;
                _settingAllianceValue = true;
                btnNone.Visible = true;
                btnFriendly.Visible = true;
                btnIntel.Visible = true;
                btnFormal.Visible = true;
                AllianceLevel myStance = Game.CurrentPlayer.AlliedWith(p);
                btnNone.Down = myStance == AllianceLevel.None;
                btnFriendly.Down = myStance == AllianceLevel.NonAggression;
                btnIntel.Down = myStance == AllianceLevel.Visibility;
                btnFormal.Down = myStance == AllianceLevel.Formal;
                Refresh();
                _settingAllianceValue = false;
            }
        }

        //changing alliance!
        private void btnNone_DownStateChanged(object sender, EventArgs e)
        {
            if (_settingAllianceValue)
                return;
            StateButton btn = sender as StateButton;
            if (!btn.Down)
                return;
            string s = btn.Tag as string;
            Player p = lvPlayers.SelectedItems[0].Tag as Player;
            AllianceLevel stance = (AllianceLevel)Int32.Parse(s);
            GamePage.SubmitOrder(OrderType.Alliance, Game.CurrentPlayer, p, stance);
        }
    }
}
